﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace WebCommon.Bootstrap
{
    /// <summary>
    /// ace自定义控件
    /// </summary>


    public static class Html
    {
        /// <summary>
        /// 下拉选择框
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="P"></typeparam>
        /// <param name="html"></param>
        /// <param name="expression"></param>
        /// <param name="selectListItems"></param>
        /// <param name="obj"></param>
        /// <param name="col"></param>
        /// <param name="col2"></param>
        /// <returns></returns>
        public static MvcHtmlString BsSelect<T, P>(this HtmlHelper<T> html, Expression<Func<T, P>> expression, IEnumerable<SelectListItem> selectListItems, object obj = null, int col = 2, int col2 = 6)
        {

            if (obj == null)
            {
                obj = new { @class = "form-control" };
            }
            var intcol = 12 - col - col2;
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<div class=\"form-group\">");


            sb.AppendLine(html.LabelFor(expression, new { @class = "control-label col-sm-3 col-md-" + col }).ToHtmlString());

            sb.AppendLine("<div class=\"col-sm-6 col-md-" + col2 + "\">");
            sb.AppendLine(html.DropDownListFor(expression, selectListItems, obj).ToHtmlString());
            sb.AppendLine(" </div>");
            sb.AppendLine("<div class=\"   text-danger col-md-" + intcol + " \">");
            sb.AppendLine(html.ValidationMessageFor(expression).ToHtmlString());
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");

            return MvcHtmlString.Create(sb.ToString());
        }
        /// <summary>
        /// 显示字段的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="P"></typeparam>
        /// <param name="html"></param>
        /// <param name="expression"></param>
        /// <param name="selectListItems"></param>
        /// <param name="obj"></param>
        /// <param name="col"></param>
        /// <param name="col2"></param>
        /// <returns></returns>
        public static MvcHtmlString BsLabel<T, P>(this HtmlHelper<T> html, Expression<Func<T, P>> expression,
           int col = 2, int col2 = 6)
        {

            var intcol = 12 - col - col2;
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<div class=\"form-group\">");


            sb.AppendLine(html.LabelFor(expression, new { @class = "control-label col-sm-3 col-md-" + col }).ToHtmlString());
            sb.AppendLine("<div class=\"col-sm-6 col-md-" + col2 + " form-control-static\" >");
            sb.AppendLine(html.ValueFor(expression).ToString());
            sb.AppendLine(" </div>");
            sb.AppendLine("<div class=\"hide-sm-only text-danger \">");
            sb.AppendLine(html.ValidationMessageFor(expression).ToHtmlString());
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");

            return MvcHtmlString.Create(sb.ToString());

        }

        /// <summary>
        /// 提交按钮
        /// </summary> 
        /// <param name="html"></param> 
        /// <param name="btnName"></param>
        /// <param name="col"></param>
        /// <param name="col2"></param>
        /// <returns></returns>
        public static MvcHtmlString BsBtn(this HtmlHelper html, string btnName = "确定", int col = 2, int col2 = 6)
        {
            var intcol = 12 - col - col2;
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<div class=\"form-group\">");
            sb.AppendLine("<div class=\"col-sm-offset-" + col + " col-sm-" + col2 + "\"> ");
            sb.AppendLine(" <button type = \"submit\" class=\"btn btn-default\">" + btnName + "</button> ");

            sb.AppendLine("</div>");
            sb.AppendLine("</div>");

            return MvcHtmlString.Create(sb.ToString());
        }

        /// <summary>
        /// 提交按钮
        /// </summary> 
        /// <param name="html"></param>
        /// <param name="actionName"></param>
        /// <param name="controllerName"></param>
        /// <param name="btnName"></param>      
        /// <returns></returns>
        public static MvcHtmlString BsLink(this HtmlHelper html, string actionName, string controllerName, string btnName = "确定")
        {
            return MvcHtmlString.Create(html.ActionLink(btnName, actionName, controllerName).ToString());
        }

        /// <summary>
        /// 输入框
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="P"></typeparam>
        /// <param name="html"></param>
        /// <param name="expression"></param>
        /// <param name="placeholder"></param>
        /// <param name="obj"></param>
        /// <param name="col"></param>　
        /// <returns></returns>
        public static MvcHtmlString BsText<T, P>(this HtmlHelper<T> html, Expression<Func<T, P>> expression, string placeholder = "", object obj = null, int col = 2)
        {
            if (obj == null)
            {
                obj = new { @class = "form-control", placeholder = placeholder };
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<div class=\"form-group\">");


            sb.AppendLine(html.LabelFor(expression, new { @class = "control-label col-sm-3 col-md-" + col }).ToHtmlString());
            sb.AppendLine("<div class=\"col-sm-6 col-md-6\">");
            sb.AppendLine(html.TextBoxFor(expression, obj).ToHtmlString());
            sb.AppendLine(" </div>");
            sb.AppendLine("<div class=\"hide-sm-only text-danger \">");
            sb.AppendLine(html.ValidationMessageFor(expression).ToHtmlString());
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");

            return MvcHtmlString.Create(sb.ToString());
        }

        /// <summary>
        /// 单选框
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="P"></typeparam>
        /// <param name="html"></param>
        /// <param name="expression"></param>
        /// <param name="col"></param>　
        /// <returns></returns>
        public static MvcHtmlString BsRadio<T, P>(this HtmlHelper<T> html, Expression<Func<T, P>> expression, Dictionary<string, string> vals, int col = 2)
        {

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<div class=\"form-group\">");


            sb.AppendLine(html.LabelFor(expression, new { @class = "control-label col-sm-3 col-md-" + col }).ToHtmlString());
            sb.AppendLine("<div class=\"col-sm-6 col-md-6\">");

            foreach (var item in vals)
            {
                sb.AppendLine("<label class=\"radio-inline\">");
                sb.AppendLine(html.RadioButtonFor(expression, item.Key).ToHtmlString());
                sb.AppendLine(item.Value);
                sb.AppendLine("</label>");
            }


            sb.AppendLine(" </div>");
            sb.AppendLine("<div class=\"hide-sm-only text-danger \">");
            sb.AppendLine(html.ValidationMessageFor(expression).ToHtmlString());
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");

            return MvcHtmlString.Create(sb.ToString());
        }

        /// <summary>
        /// 输入框(时间)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="P"></typeparam>
        /// <param name="html"></param>
        /// <param name="expression"></param>
        /// <param name="format"></param>
        /// <param name="obj"></param>
        /// <param name="col"></param>
        /// <param name="col2"></param>
        /// <returns></returns>
        public static MvcHtmlString BsDate<T>(this HtmlHelper<T> html, Expression<Func<T, DateTime>> expression, string format = "yyyy/mm/dd", object obj = null, int col = 2, int col2 = 6)
        {
            if (obj == null)
            {
                obj = new { @class = "form-control " };
            }
            var intcol = 12 - col - col2;
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<div class=\"form-group\">");


            sb.AppendLine(html.LabelFor(expression, new { @class = "control-label col-sm-3 col-md-" + col }).ToHtmlString());
            sb.AppendLine("<div class=\"col-sm-6 col-md-" + col2 + "\">");
            sb.AppendLine($"<div  data-date-format=\"{format}\" data-date=\"{html.ValueFor(expression)}\" class=\"input-group date form_datetime \">");
            sb.AppendLine("<input type=\"text\"  readonly=\"\" value=\"\" size=\"16\" class=\"form-control\">");
            sb.AppendLine("<span class=\"input-group-addon\"><span class=\"glyphicon glyphicon-remove\"></span></span>");
            sb.AppendLine("<span class=\"input-group-addon\"><span class=\"glyphicon glyphicon-th\"></span></span>");


            sb.AppendLine(" </div>");
            sb.AppendLine(html.HiddenFor(expression).ToHtmlString());
            sb.AppendLine(" </div>");
            sb.AppendLine("<div class=\"hide-sm-only text-danger \">");
            sb.AppendLine(html.ValidationMessageFor(expression).ToHtmlString());
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");

            return MvcHtmlString.Create(sb.ToString());
        }

        /// <summary>
        /// 勾选框
        /// </summary>
        /// <param name="html"></param>
        /// <param name="expression"></param>
        /// <param name="obj"></param>
        /// <param name="col"></param>
        /// <param name="col2"></param>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="P"></typeparam>
        /// <returns></returns>
        public static MvcHtmlString BsCheckbox<T>(this HtmlHelper<T> html, Expression<Func<T, bool>> expression, object obj = null, int col = 2, int col2 = 6)
        {
            if (obj == null)
            {
                obj = new { @class = "form-control" };
            }
            var intcol = 12 - col - col2;
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<div class=\"form-group\">");

            sb.AppendLine("<div class=\"col-sm-6 col-sm-offset-3 col-md-" + col2 + " col-md-offset-" + col + " \">");
            sb.AppendLine("<label>");

            sb.AppendLine(html.CheckBoxFor(expression).ToHtmlString());
            sb.AppendLine(html.DisplayNameFor(expression).ToHtmlString());
            sb.AppendLine("</label>");
            sb.AppendLine(" </div>");
            sb.AppendLine("<div class=\"hide-sm-only text-danger \">");
            sb.AppendLine(html.ValidationMessageFor(expression).ToHtmlString());
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");

            return MvcHtmlString.Create(sb.ToString());
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="html"></param>
        /// <param name="fileid"></param>
        /// <param name="multiple"></param>
        /// <param name="col"></param>
        /// <returns></returns>
        public static MvcHtmlString BsUpImg<T>(this HtmlHelper<T> html, string fileid = "file", bool multiple = false, int col = 2)
        {
            var col2 = 12-col;
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<div class=\"form-group\">");
            sb.AppendLine($"<label class=\"control-label col-sm-3 col-md-2\" for=\"{fileid}\">上传文件</label>");
            sb.AppendLine($"<div class=\"col-sm-9  col-md-{ col2 }  \">");
            sb.AppendLine("<input type =\"file\" id=\"file\" " + (multiple ? "multiple=\"\"" : "") + ">");
            sb.AppendLine(" </div>");
            sb.AppendLine(" </div>");

            sb.AppendLine("<div class=\"form-group\">");
            sb.AppendLine($"<label class=\"control-label col-sm-3 col-md-2\" for=\"{fileid}\">预览</label>");
            sb.AppendLine($"<div id=\"imgs{fileid}\" class=\"col-sm-9 col-md-{ col2 } \">");

            sb.AppendLine(" </div>");
            sb.AppendLine(" </div>");

            return MvcHtmlString.Create(sb.ToString());
        }

        /// <summary>
        /// Madol模态框
        /// </summary>
        /// <param name="html"></param>
        /// <param name="id"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static MvcBsModal BsMadol(this HtmlHelper html,string id, string title="")
        {
            var mvcForm = new MvcBsModal(html.ViewContext,id,title);
         
            return mvcForm;

        }


    }


    /// <summary>
    /// Modal模态框
    /// </summary>
    public class MvcBsModal : IDisposable
    {
        /// <summary>
        /// 模态框标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// ID
        /// </summary>
        public string Id { get; set; }  

        readonly ViewContext _viewContext;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="viewContext"></param>
        public MvcBsModal(ViewContext viewContext, string id, string title)
        {
            this.Id = id;
            this.Title = title;

            _viewContext = viewContext;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine($"<div class=\"modal fade\" id=\"{Id}\"  tabindex=\"-1\"  role=\"dialog\" aria-labelledby=\"mL{Id}\">");
            sb.AppendLine("<div class=\"modal-dialog\" role=\"document\">");
            sb.AppendLine("<div class=\"modal-content\">");

            sb.AppendLine("<div class=\"modal-header\">");
            sb.AppendLine("<button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>");
            if (!string.IsNullOrWhiteSpace(Title))
            {
                sb.AppendLine($"<h4 class=\"modal-title\" id=\"mL{Id}\">{Title}</h4>");
            } 
            sb.AppendLine("</div>");
            sb.AppendLine("<div class=\"modal-body\">");

            _viewContext.Writer.WriteLine(sb.ToString());
        }

       


        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
                 
            StringBuilder sb = new StringBuilder();

            //sb.AppendLine("< div class=\"modal-footer\">");
            //sb.AppendLine("<button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Close</button>");
            //sb.AppendLine("</div >");
            sb.AppendLine("</div >");
            sb.AppendLine("</div >");
            sb.AppendLine("</div >");
            sb.AppendLine("</div >");
            _viewContext.Writer.WriteLine(sb.ToString());
        }
    }

}
